//
//  XWXSqliteModelTool.m
//  XWXSqlite
//
//  Created by 许卫星 on 17/9/4.
//  Copyright © 2017年 wxxu. All rights reserved.
//

#import "XWXSqliteQueryCommandTool.h"
#import "XWXSqliteModelTool.h"
#import "XWXSqliteQueryCommand+Table.h"
#import "XWXSqliteQueryCommand+Query.h"
#import "XWXSqliteQueryCommand+IUD.h"

@implementation XWXSqliteQueryCommandTool

#pragma mark - 查询所有纪录
+ (XWXSqliteQueryCommand *)selectWithModelClass: (Class)cls withDBName: (NSString *)dbName
{
    return [self selectWithModelClass:cls withDBName:dbName isDistinct:NO];
}

+ (XWXSqliteQueryCommand *)selectWithModelClass: (Class)cls withDBName: (NSString *)dbName isDistinct:(BOOL)isDistinct
{
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:cls];
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    [[queryCommand select:nil isDistinct:isDistinct] from:tableName];
    
    return queryCommand;
}


+ (XWXSqliteQueryCommand *)selectWithModelClass: (Class)cls withDBName: (NSString *)dbName columList:(NSString *)columList isDistinct:(BOOL)isDistinct
{
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:cls];
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    [[queryCommand select:columList isDistinct:isDistinct] from:tableName];
    
    return queryCommand;
}

+ (XWXSqliteQueryCommand *)selectWithModelClass: (Class)cls withDBName: (NSString *)dbName columList:(NSString *)columList columnName:(NSString *)columnName relation:(ColumnNameToValueRelationType)relationType value:(id)value isDistinct:(BOOL)isDistinct
{
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:cls];
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    [[[queryCommand select:columList isDistinct:isDistinct] from:tableName] whereColumnName:columnName relation:relationType value:value valueType:ColumnValueRelationTextType];
    
    return queryCommand;
}

#pragma mark - IUD
+ (XWXSqliteQueryCommand *)insertWithModel: (id)model withDBName: (NSString *)dbName
{
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:[model class]];
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    NSDictionary *columDataDic = [XWXSqliteModelTool modelIvarNameValueDic:model];
    [queryCommand insertTable:tableName columDataDic:columDataDic];
    
    return queryCommand;
}

+ (XWXSqliteQueryCommand *)updateWithModel: (id)model withDBName: (NSString *)dbName whereCondition:(NSString *)condition
{
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:[model class]];
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    NSDictionary *updateDataDic = [XWXSqliteModelTool updateModelIvarNameValueDic:model];
    
    [queryCommand updateTable:tableName columData:updateDataDic whereCondition:condition];
    
    return queryCommand;
}

+ (XWXSqliteQueryCommand *)updateWithModel: (id)model withDBName: (NSString *)dbName columnName:(NSString *)columnName relation:(ColumnNameToValueRelationType)relationType value:(id)value
{
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:[model class]];
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    NSDictionary *updateDataDic = [XWXSqliteModelTool updateModelIvarNameValueDic:model];
    
    [[queryCommand updateTable:tableName columData:updateDataDic whereCondition:nil] whereColumnName:columnName relation:relationType value:value valueType:ColumnValueRelationTextType];
    
    return queryCommand;
}

+ (XWXSqliteQueryCommand *)deleteWithModelClass: (Class)cls withDBName: (NSString *)dbName whereCondition:(NSString *)condition
{
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:cls];
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    
    [queryCommand deleteTable:tableName whereCondition:condition];
    return queryCommand;
}

+ (XWXSqliteQueryCommand *)deleteWithModelClass: (Class)cls withDBName: (NSString *)dbName columnName:(NSString *)columnName relation:(ColumnNameToValueRelationType)relationType value:(id)value
{
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:cls];
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    
    [[queryCommand deleteTable:tableName whereCondition:nil] whereColumnName:columnName relation:relationType value:value valueType:ColumnValueRelationTextType];
    return queryCommand;
}

#pragma mark - 表格
+ (XWXSqliteQueryCommand *)createTableWithModelClass: (Class)cls withDBName: (NSString *)dbName;
{
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:cls];
    NSString *columnInfo = [XWXSqliteModelTool columnNamesAndTypesStr:cls];
    NSString *primaryKey = [XWXSqliteModelTool primaryKeyWithCls:cls];
    [queryCommand createTable:tableName columnInfo:columnInfo primaryKey:primaryKey];
    
    return queryCommand;
}

+ (XWXSqliteQueryCommand *)createTempTableWithModelClass: (Class)cls withDBName: (NSString *)dbName
{
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    NSString *tableName = [XWXSqliteModelTool tempTableNameWithModelClass:cls];
    NSString *columnInfo = [XWXSqliteModelTool columnNamesAndTypesStr:cls];
    NSString *primaryKey = [XWXSqliteModelTool primaryKeyWithCls:cls];
    [queryCommand createTable:tableName columnInfo:columnInfo primaryKey:primaryKey];
    
    return queryCommand;
}

+ (XWXSqliteQueryCommand *)dropTableWithTableName: (NSString *)tableName withDBName: (NSString *)dbName
{
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    [queryCommand dropTable:tableName];
    
    return queryCommand;
}

+ (XWXSqliteQueryCommand *)dropTableWithModelClass: (Class)cls withDBName: (NSString *)dbName;
{
    NSString *tableName = [XWXSqliteModelTool tableNameWithModelClass:cls];
    XWXSqliteQueryCommand *queryCommand = [[XWXSqliteQueryCommand alloc] initWithDatabaseName:dbName];
    [queryCommand dropTable:tableName];
    
    return queryCommand;
}

@end
